﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
using MyWeb.Business;
using MyWeb.Common;
using MyWeb.Data;
using System.Data;


namespace MyWeb
{
    public class Global : System.Web.HttpApplication
    {
        void RegisterRoutes(RouteCollection routes)
        {
            //login
            RouteTable.Routes.MapPageRoute("Login", "quangminh", "~/Control/Login/Login.aspx");
            //chitietsp
            RouteTable.Routes.MapPageRoute("Product", "Pro/{GrId}/{Id}/{Name}.aspx", "~/Module/Product/DetailPro.aspx");
            //chitietnhomsp
            RouteTable.Routes.MapPageRoute("GrProduct", "GrProducts/{Id}/{Name}.aspx", "~/Module/Product/DetailGroup.aspx");
            //nhomtin
            RouteTable.Routes.MapPageRoute("GrNews", "GrNews/{Id}/{Name}.aspx", "~/Module/News/GrNews.aspx");
            //tin
            RouteTable.Routes.MapPageRoute("News", "News/{GrId}/{Id}/{Name}.aspx", "~/Module/News/NewsDetail.aspx");                        
            //searchsp
            RouteTable.Routes.MapPageRoute("Searchsp", "search/{Name}.aspx", "~/Module/Search/Ketquasp.aspx");
            //gio hang
            RouteTable.Routes.MapPageRoute("Shop", "Shop/{Id}.aspx", "~/Module/Shop/GioHang.aspx");
        }

        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
            // Code that runs on application startup
            try
            {
                Application["HomNay"] = 0;
                Application["HomQua"] = 0;
                Application["TuanNay"] = 0;
                Application["TuanTruoc"] = 0;
                Application["ThangNay"] = 0;
                Application["ThangTruoc"] = 0;
                Application["TatCa"] = 0;
                Application["visitors_online"] = 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        void Application_End(object sender, EventArgs e)
        {

            //  Code that runs on application shutdown
            int i = (int)Application["Online"];
            if (i > 1)
            {
                Application["Online"] = i - 1;
            }
        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            if (Application["Online"] == null)
            {
                Application["Online"] = 1;
            }
            else
            {
                Application["Online"] = (int)Application["Online"] + 1;
            }
            try
            {
                DataTable dt = VisitorService.Visitor_GetByAll();
                if (dt.Rows.Count > 0)
                {
                    Application["HomNay"] = Int32.Parse("0" + dt.Rows[0]["HomNay"].ToString());
                    Application["HomQua"] = Int32.Parse("0" + dt.Rows[0]["HomQua"].ToString());
                    Application["TuanNay"] = Int32.Parse("0" + dt.Rows[0]["TuanNay"].ToString());
                    Application["TuanTruoc"] = Int32.Parse("0" + dt.Rows[0]["TuanTruoc"].ToString());
                    Application["ThangNay"] = Int32.Parse("0" + dt.Rows[0]["ThangNay"].ToString());
                    Application["ThangTruoc"] = Int32.Parse("0" + dt.Rows[0]["ThangTruoc"].ToString());
                    Application["TatCa"] = Int32.Parse("0" + dt.Rows[0]["TatCa"].ToString());
                }
                dt.Dispose();
            }
            catch { }

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            int i = (int)Application["Online"];
            if (i > 1)
            {
                Application["Online"] = i - 1;
            }
        }

    }
}
